﻿/// <reference path="../../../fly.common/fly/fly.js" />
!function () {
    var $ = fly
    window.indexPath = '../'
    fly.box.Share = $.Class({
        constructor: function () {
            var me = this;
            $(function () {
                me.fileList = $('#field-list span')
                me.toOrgSpace = $('#to-org-space');
                me.getFiles()
            });
        },
        showFiles: function () {
            var html = this.files.select(function (f) {
                return '<a id={id} title="{name}"><nobr>{name}</nobr><b title=删除 onclick="fly(this.parentNode).remove()">x</b></a>'.format(f)
            })
            this.fileList.html(html.join(""));
        },
        getFiles: function () {
            var me = this;
            this.files = window.frameElement ? frameElement.shareFiles : null;
            if (this.files) {
                this.showFiles();
            } else {
                var ids = $.getQuery('ids');
                if (ids) {
                    $.post(fly.box.ajaxUrl('Share', 'GetFileInfo'),
                        { ids: ids },
                        function (result) {
                            me.files = result.data
                            me.showFiles()
                        }
                    )
                }
            }
        },
        getFileIds: function (noSelected) {
            var ids = this.fileList.find('a').select('o=>o.fileId||o.id').join(',')
            if (!ids) {
                $.alert(noSelected || '没有要分享的文件。')
                return null
            }
            return ids;
        },
        unToShareSpace: function () {
            var ids = this.getFileIds('没有要取出的文件。');
            if (!ids)
                return;
            frameElement.shared = true;
            $.post({
                url: fly.box.ajaxUrl('Share', 'UnToShareSpace'),
                data: { 'file-ids': ids },
                loading: {
                    css: 'f-loading',
                    box: $('.ajax-loading')
                },
                success: function (result) {
                    if (result.success) {
                        setTimeout(function () {
                            frameElement.dialog.close();
                        });
                    }
                    else
                        $.alert(result.msg)
                }
            });
        },
        submit: function (callback) {
            if (!this.toUser && !this.toShareSpace) {
                $.alert('请选择分享类型。');
                return false;
            }

            var ids = this.getFileIds()
            if (!ids) {
                return false
            }

            var data = {
                'file-ids': ids,
                summary: $('#summary').val(),
                'need-login': $('#need-login')[0].checked,
                target: $('#target').val()
            };

            if (this.toUser && !data.target) {
                $.alert('请输入要分享的用户');
                return false
            }

            frameElement.shared = true;
            $.post({
                url: fly.box.ajaxUrl('Share', this.toUser ? 'ShareToUser' : 'ToShareSpace'),
                data: data,
                loading: {
                    css: 'f-loading',
                    box: $('.ajax-loading')
                },
                success: function (result) {
                    if (result.success) {
                        if (callback)
                            callback()
                    }
                    else
                        $.alert(result.msg)
                }
            });
        }
    });

    window.share = new fly.box.Share();
    window.submit = function (callback) {
        return share.submit(callback);
    }
} ()